package com.lening.community.service.impl;


import com.lening.common.thread.ThreadGroup;
import com.lening.common.web.Request;
import com.lening.community.service.*;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;


@Service
public class WorkbenchImpl implements WorkbenchService {

    @Resource
    private CommunityService communityService;
    @Resource
    private BuildingService buildingService;
    @Resource
    private HouseService houseService;
    @Resource
    private ShopsService shopsService;
    @Resource
    private CarAreaService carAreaService;
    //    @Override
//    public Map<String, Object> getWorkbenchData()  {
////        ExecutorService threadPool = ThreadGroups.fixed;
////        Object complain = CompletableFuture.supplyAsync(() -> Request.syncGet("community-management/property/complain/getStateFalseCount", Object.class), threadPool).join();
//
////        //投诉未处理个数
    //Object complain = Request.asyncGet("property-community/complaSuggestionMainTables/count", Object.class);
    ////        //保修个数
////        Object repair = Request.asyncGet("community-management/property/repair/getStateFalseCount", Object.class);
////        //工单个数
////        Object work = Request.asyncGet("community-management/property/work/getStateFalseCount", Object.class);
////        //待审核业主
////        Object owner = Request.asyncGet("community-management/owner/owner/getStateFalseCount", Object.class);
////        //待审核家属
////        Object family = Request.asyncGet("community-management/owner/family-members/getStateFalseCount", Object.class);
//
//        //社区个数
//        Object community = Request.asyncGet("property-community/complaSubtables/page", Object.class);
////        //楼宇个数
////        Object floor = Request.asyncGet("community-management/community/floor/getCount", Object.class);
////        //房屋个数
////        Object house = Request.asyncGet("community-management/community/house/getCount", Object.class);
////        //商铺个数
////        Object shop = Request.asyncGet("community-management/community/shop/getCount", Object.class);
////        //车位个数
////        Object stall = Request.asyncGet("community-management/community/stall/getCount", Object.class);
//
   // Map<String, Object> result = new HashMap<>();
      // result.put("complain",complain);
////        result.put("repair",repair);
////        result.put("work",work);
////        result.put("owner",owner);
////        result.put("family",family);
//
//        result.put("community",community);
////        result.put("floor",floor);
////        result.put("house",house);
////        result.put("shop",shop);
////        result.put("stall",stall);
//        return result;
//
//    }


    @Override
    public Map<String, Object> getWorkbenchData() throws ExecutionException, InterruptedException {
        //投诉未处理个数
        Object complain = Request.asyncGet("property-community/complaSuggestionMainTables/count", Object.class);
        Map<String, Object> result = new HashMap<>();
        result.put("complain", complain);
        return result;
    }

    @Override
    public Map getAll() {
        //使用线程池执行
        ExecutorService threadPool = ThreadGroup.FIXED_THREAD_POOL;

        /*
         * 异步获取社区个数
         */
        CompletableFuture community = CompletableFuture.supplyAsync(() -> {
            return communityService.getCount();
        }, threadPool);

        /*
         * 异步获取楼宇个数
         */
        CompletableFuture floor = CompletableFuture.supplyAsync(() -> {
            return buildingService.getCount();
        }, threadPool);


        /*
         * 异步获取房屋个数
         */
        CompletableFuture house = CompletableFuture.supplyAsync(() -> {
            return houseService.getCount();
        }, threadPool);

        /*
         * 异步获取商铺个数
         */
        CompletableFuture shop = CompletableFuture.supplyAsync(() -> {
            return shopsService.getCount();
        }, threadPool);

        /*
         * 异步获取车位个数
         */
        CompletableFuture carArea = CompletableFuture.supplyAsync(() -> {
            return carAreaService.getCount();
        }, threadPool);

        //等待线程执行完成
        CompletableFuture.allOf(community, floor, house, shop, carArea).join();


        HashMap<Object, Object> result = new HashMap<>();

        try {
            result.put("community", community.get());
            result.put("floor", floor.get());
            result.put("house", house.get());
            result.put("shop", shop.get());
            result.put("carArea", carArea.get());

        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;

    }
}
